﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace HugeFlow.CommandPattern.Commands
{
    /// <summary>
    ///  class for StaticCommand and CommandParameter's binding
    /// </summary>
    internal class StaticCommandBinder : CommandBinderBase
    {
        #region CommandName
        /// <summary> 
        /// Gets or sets the CommandName possible Value of the string object.
        /// </summary> 
        public string CommandName
        {
            get { return (string)GetValue(CommandNameProperty); }
            set { SetValue(CommandNameProperty, value); }
        }

        /// <summary> 
        /// Identifies the CommandName dependency property.
        /// </summary> 
        public static readonly DependencyProperty CommandNameProperty =
                    DependencyProperty.Register(
                          "CommandName",
                          typeof(string),
                          typeof(StaticCommandBinder),
                          new PropertyMetadata(OnCommandNamePropertyChanged));

        /// <summary>
        /// CommandNameProperty property changed handler. 
        /// </summary>
        /// <param name="d">StaticActionCommand that changed its CommandName.</param>
        /// <param name="e">DependencyPropertyChangedEventArgs.</param> 
        private static void OnCommandNamePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            StaticCommandBinder _staticActionCommand = d as StaticCommandBinder;
            if (_staticActionCommand != null)
            {
                if (e.NewValue != null)
                    _staticActionCommand._currentCommand = StaticCommand.FindCommand((string)e.NewValue);
                else
                    _staticActionCommand._currentCommand = null;
            }
        }
        #endregion CommandName

        StaticCommand _currentCommand;

        #region ICommand Members

        public override bool CanExecute(object parameter)
        {
            if (_currentCommand == null)
                return false;
            else
                return true;
        }

        public override void Execute(object parameter)
        {
            if (parameter == null)
                parameter = InstantCommandParameter;

            _currentCommand.Execute(parameter);
        }

        #endregion
    }
}
